<?php
/**
 * @Author: gan
 * @Description: 阿里云oss
 * @File:  OssAdapter
 * @Version: 1.0.0
 * @Date: 2021/12/27 9:15 上午
 */
declare(strict_types=1);

namespace RX\Component\Upload\Impl;

use AlibabaCloud\Client\AlibabaCloud;
use RX\Enums\UploadEnum;
use RX\Exception\BizException;
use RX\Helpers\StringHelper;
use OSS\OssClient;

class OssImpl implements UploadInterface
{
    private string $regionId = "cn-shanghai";
    private string $accessKeyId;
    private string $accessSecret;
    private string $bucket;
    private string $region;
    private string $roleArn;
    private string $domain;
    private string $endpoint;


    public function __construct(array $config = [])
    {
        if (empty($config)) {
            $config = StringHelper::config('upload.oss');
        }
        $this->accessKeyId  = $config["accessKeyId"];
        $this->accessSecret = $config["accessSecret"];
        $this->bucket       = $config["bucket"];
        $this->roleArn      = $config["roleArn"];
        $this->domain       = $config["domain"];
        $this->region       = $config["region"];
        $this->endpoint     = $config["endpoint"];
    }

    public function stsData()
    {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)->regionId($this->regionId)->asDefaultClient();

        $result = AlibabaCloud::rpc()
                              ->product('Sts')
                              ->scheme('https')
                              ->version('2015-04-01')
                              ->action('AssumeRole')
                              ->method('POST')
                              ->host('sts.aliyuncs.com')
                              ->options([
                                  'query' => [
                                      'RoleArn'         => $this->roleArn,
                                      'RoleSessionName' => 'front_end',
                                  ],
                              ])
                              ->request();

        $resultData = $result->toArray();

        $accessKeyId     = $resultData['Credentials']['AccessKeyId'] ?? null;
        $accessKeySecret = $resultData['Credentials']['AccessKeySecret'] ?? null;
        $securityToken   = $resultData['Credentials']['SecurityToken'] ?? null;
        if (!$accessKeyId || !$accessKeySecret || !$securityToken) {
            throw new BizException(UploadEnum::class, UploadEnum::GET_STS_ERROR);
        }

        return [
            'accessKeyId'     => $accessKeyId,
            'accessKeySecret' => $accessKeySecret,
            'stsToken'        => $securityToken,
            'bucket'          => $this->bucket,
            'region'          => $this->region,
        ];
    }


    public function uploadFile(array $params)
    {
        $object   = $params["object"] ?? null;
        $filePath = $params["filePath"] ?? null;
        if (empty($object) || empty($filePath)) {
            throw new BizException(UploadEnum::class, UploadEnum::PUT_OBJECT_PARAMS_ERROR);
        }
        $ossClient = new OssClient($this->accessKeyId, $this->accessSecret, $this->endpoint);
        return $ossClient->uploadFile($this->bucket, $object, $filePath);
    }

    public function domain()
    {
        return $this->domain;
    }
}
